home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / packages / terminal.el.z / terminal.el
Encoding:
Text File  |  1998-05-21  |  44.0 KB  |  1,281 lines

  1. ;;; terminal.el --- terminal emulator for GNU Emacs.
  2. ;; Keywords: comm, terminals
  3.  
  4. ;; Copyright (C) 1986, 1987, 1988, 1989, 1993 Free Software Foundation, Inc.
  5. ;; Written by Richard Mlynarik, November 1986.
  6. ;; Face and attribute support added by Richard Mlynarik, April 1996.
  7.  
  8. ;; This file is part of XEmacs.
  9.  
  10. ;; XEmacs is free software; you can redistribute it and/or modify it
  11. ;; under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; XEmacs is distributed in the hope that it will be useful, but
  16. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18. ;; General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with XEmacs; see the file COPYING.  If not, write to the 
  22. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. ;; Boston, MA 02111-1307, USA.
  24.  
  25. ;;; Synched up with: Not synched with FSF.
  26.  
  27. ;;#### TODO
  28. ;;#### terminfo?
  29.  
  30. ;;#### One probably wants to do setenv MORE -c when running with
  31. ;;####   more-processing enabled.
  32.  
  33. (provide 'terminal)
  34. (require 'ehelp)
  35.  
  36. (defvar terminal-escape-char ?\C-^
  37.   "*All characters except for this are passed verbatim through the
  38. terminal-emulator.  This character acts as a prefix for commands
  39. to the emulator program itself.  Type this character twice to send
  40. it through the emulator.  Type ? after typing it for a list of
  41. possible commands.
  42. This variable is local to each terminal-emulator buffer.")
  43.  
  44. (defvar terminal-scrolling t
  45.   "*If non-nil, the terminal-emulator will `scroll' when output occurs
  46. past the bottom of the screen.  If nil, output will `wrap' to the top
  47. of the screen.
  48. This variable is local to each terminal-emulator buffer.")
  49.  
  50. (defvar terminal-more-processing t
  51.   "*If non-nil, do more-processing.
  52. This variable is local to each terminal-emulator buffer.")
  53.  
  54. ;; If you are the sort of loser who uses scrolling without more breaks
  55. ;; and expects to actually see anything, you should probably set this to
  56. ;; around 400
  57. (defvar terminal-redisplay-interval 5000
  58.   "*Maximum number of characters which will be processed by the
  59. terminal-emulator before a screen redisplay is forced.
  60. Set this to a large value for greater throughput,
  61. set it smaller for more frequent updates but overall slower
  62. performance.")
  63.  
  64. (defvar terminal-more-break-insertion
  65.   "*** More break -- Press space to continue ***")
  66.  
  67. (defvar terminal-escape-map nil)
  68. (defvar terminal-map nil)
  69. (defvar terminal-more-break-map nil)
  70. (if terminal-map
  71.     nil
  72.   (let ((map (make-keymap)))
  73.     (set-keymap-name map 'terminal-map)
  74.  
  75.     (let ((meta-prefix-char -1)
  76.           (s (make-string 1 0))
  77.           (i 0))
  78.       (while (< i 256)
  79.         (aset s 0 i)
  80.         (define-key map s 'te-pass-through)
  81.         (setq i (1+ i))))
  82.  
  83.     ;(define-key map "\C-l"
  84.     ;  '(lambda () (interactive) (te-pass-through) (redraw-display)))
  85.     (setq terminal-map map)))
  86.  
  87. (if terminal-escape-map
  88.     nil
  89.   (let ((map (make-keymap)))
  90.     (set-keymap-name map 'terminal-escape-map)
  91.     (let ((s (make-string 1 ?0)))
  92.       (while (<= (aref s 0) ?9)
  93.     (define-key map s 'digit-argument)
  94.     (aset s 0 (1+ (aref s 0)))))
  95.     (define-key map "b" 'switch-to-buffer)
  96.     (define-key map "o" 'other-window)
  97.     (define-key map "e" 'te-set-escape-char)
  98.     (define-key map "\C-l" 'redraw-display)
  99.     (define-key map "\C-o" 'te-flush-pending-output)
  100.     (define-key map "m" 'te-toggle-more-processing)
  101.     (define-key map "x" 'te-escape-extended-command)
  102.     (define-key map "?" 'te-escape-help)
  103.     (define-key map (vector help-char) 'te-escape-help)
  104.     (setq terminal-escape-map map)))
  105.  
  106. (defvar te-escape-command-alist '())
  107. (if te-escape-command-alist
  108.     nil
  109.   (setq te-escape-command-alist
  110.     '(("Set Escape Character" . te-set-escape-char)
  111.       ("Refresh" . redraw-display)
  112.       ("Record Output" . te-set-output-log)
  113.       ("Photo" . te-set-output-log)
  114.       ("Tofu" . te-tofu) ;; confuse the uninitiated
  115.       ("Stuff Input" . te-stuff-string)
  116.       ("Flush Pending Output" . te-flush-pending-output)
  117.       ("Enable More Processing" . te-enable-more-processing)
  118.       ("Disable More Processing" . te-disable-more-processing)
  119.       ("Scroll at end of page" . te-do-scrolling)
  120.       ("Wrap at end of page" . te-do-wrapping)
  121.       ("Switch To Buffer" . switch-to-buffer)
  122.       ("Other Window" . other-window)
  123.       ("Kill Buffer" . kill-buffer)
  124.       ("Help" . te-escape-help)
  125.       ("Set Redisplay Interval" . te-set-redisplay-interval)
  126.       )))
  127.  
  128. ;(setq terminal-more-break-map nil)
  129. (if terminal-more-break-map
  130.     nil
  131.   (let ((map (make-keymap)))
  132.     (set-keymap-name map 'terminal-more-break-map)
  133.  
  134.     (let ((meta-prefix-char -1)
  135.           (s (make-string 1 0))
  136.           (i 0))
  137.       (while (< i 256)
  138.         (aset s 0 i)
  139.         (define-key map s 'te-more-break-unwind)
  140.         (setq i (1+ i))))
  141.  
  142.     (define-key map (vector help-char) 'te-more-break-help)
  143.     (define-key map " " 'te-more-break-resume)
  144.     (define-key map "\C-l" 'redraw-display)
  145.     (define-key map "\C-o" 'te-more-break-flush-pending-output)
  146.     ;;#### this isn't right
  147.     ;(define-key map "\^?" 'te-more-break-flush-pending-output) ;DEL
  148.     (define-key map "\r" 'te-more-break-advance-one-line)
  149.  
  150.     (setq terminal-more-break-map map)))
  151.   
  152. (defvar te-width)
  153. (defvar te-height)
  154. (defvar te-process)
  155. (defvar te-pending-output)
  156. (defvar te-saved-point)
  157. (defvar te-pending-output-info)
  158. (defvar te-log-buffer)
  159. (defvar te-more-count)
  160. (defvar te-redisplay-count)
  161. (defvar te-current-face)
  162. (defvar te-current-attributes)
  163.  
  164. (make-face 'terminal-default)
  165.  
  166. (make-face 'terminal-standout)
  167. (if (not (face-differs-from-default-p 'terminal-standout))
  168.     (copy-face 'bold 'terminal-standout))
  169.  
  170. (make-face 'terminal-underline)
  171. (cond ((face-differs-from-default-p 'terminal-underline))
  172.       ((find-face 'underline)
  173.        (copy-face 'underline 'terminal-underline))
  174.       (t
  175.        (set-face-underline-p 'terminal-underline t)))
  176.  
  177. (make-face 'terminal-standout-underline)
  178. (cond ((face-differs-from-default-p 'terminal-standout-underline))
  179.       (t
  180.        (copy-face 'terminal-standout 'terminal-standout-underline)
  181.        (set-face-underline-p 'terminal-standout-underline t)))
  182.  
  183. (defun te-insert-blank (count)
  184.   (let ((p (point)))
  185.     (insert-char ?\  count)
  186.     (put-text-property p (point) 'face 'terminal-default)))
  187.  
  188.  
  189. ;;;;  escape map
  190.  
  191. (defun te-escape-p (event)
  192.   (cond ((eventp terminal-escape-char)
  193.          (cond ((key-press-event-p event)
  194.                 (and (key-press-event-p terminal-escape-char)
  195.                      (= (event-modifier-bits event)
  196.                         (event-modifier-bits terminal-escape-char))
  197.                      (eq (event-key event)
  198.                          (event-key terminal-escape-char))))
  199.                ((button-press-event-p event)
  200.                 (and (button-press-event-p terminal-escape-char)
  201.                      (= (event-modifier-bits event)
  202.                         (event-modifier-bits terminal-escape-char))
  203.                      (eq (event-button event)
  204.                          (event-button terminal-escape-char))))
  205.                (t nil)))
  206.         ((numberp terminal-escape-char)
  207.          (let ((c (event-to-character event nil t nil)))
  208.            (and c (= c terminal-escape-char))))
  209.         (t
  210.          nil)))
  211.  
  212.  
  213. (defun te-escape ()
  214.   (interactive)
  215.   (let ((c (let ((cursor-in-echo-area t)
  216.                  (prompt (if prefix-arg
  217.                              (format "Emacs Terminal escape> %d "
  218.                                      (prefix-numeric-value prefix-arg))
  219.                              "Emacs Terminal escape> ")))
  220.              (message "%s" prompt)
  221.              (let ((e (next-command-event)))
  222.                (while (button-release-event-p e)
  223.                  (setq e (next-command-event e)))
  224.                (if (te-escape-p e)
  225.                    e
  226.                    (progn
  227.                      (setq unread-command-event e)
  228.                      (lookup-key terminal-escape-map
  229.                                  (read-key-sequence prompt))))))))
  230.     (cond ((eventp c)
  231.            (message nil)
  232.            (copy-event c last-command-event)
  233.            (let ((terminal-escape-char -259))
  234.              (te-pass-through)))
  235.           (c
  236.            (call-interactively c)))))
  237.  
  238. (defun te-escape-help ()
  239.   "Provide help on commands available after terminal-escape-char is typed."
  240.   (interactive)
  241.   (message "Terminal emulator escape help...")
  242.   (let ((char (single-key-description terminal-escape-char)))
  243.     (with-electric-help
  244.       (function (lambda ()
  245.      (princ (format "Terminal-emulator escape, invoked by \"%s\"
  246. Type \"%s\" twice to send a single \"%s\" through.
  247.  
  248. Other chars following \"%s\" are interpreted as follows:\n"
  249.             char char char char))
  250.  
  251.      (princ (substitute-command-keys "\\{terminal-escape-map}\n"))
  252.      (princ (format "\nSubcommands of \"%s\" (%s)\n"
  253.             (where-is-internal 'te-escape-extended-command
  254.                        terminal-escape-map t)
  255.             'te-escape-extended-command))
  256.      (let ((l (if (fboundp 'sortcar)
  257.               (sortcar (copy-sequence te-escape-command-alist)
  258.                    'string<)
  259.               (sort (copy-sequence te-escape-command-alist)
  260.                 (function (lambda (a b)
  261.                               (string< (car a) (car b))))))))
  262.        (while l
  263.          (let ((doc (or (documentation (cdr (car l)))
  264.                 "Not documented")))
  265.            (if (string-match "\n" doc)
  266.            ;; just use first line of documentation
  267.            (setq doc (substring doc 0 (match-beginning 0))))
  268.            (princ "  \"")
  269.            (princ (car (car l)))
  270.            (princ "\":\n     ")
  271.            (princ doc)
  272.            (write-char ?\n))
  273.          (setq l (cdr l))))
  274.      nil)))))
  275.  
  276.             
  277.  
  278. (defun te-escape-extended-command ()
  279.   (interactive)
  280.   (let ((c (let ((completion-ignore-case t))
  281.          (completing-read "terminal command: "
  282.                   te-escape-command-alist
  283.                   nil t))))
  284.     (if c
  285.     (catch 'foo
  286.       (setq c (downcase c))
  287.       (let ((l te-escape-command-alist))
  288.         (while l
  289.           (if (string= c (downcase (car (car l))))
  290.           (throw 'foo (call-interactively (cdr (car l))))
  291.         (setq l (cdr l)))))))))
  292.  
  293. ;; not used.
  294. (defun te-escape-extended-command-unread ()
  295.   (interactive)
  296.   (setq unread-command-event last-command-event)
  297.   (te-escape-extended-command))
  298.  
  299. (defun te-set-escape-char (c)
  300.   "Change the terminal-emulator escape character."
  301.   (interactive (list (let ((cursor-in-echo-area t))
  302.                        (message "Set escape character to: ")
  303.                        (let ((e (next-command-event)))
  304.                          (while (button-release-event-p e)
  305.                            (setq e (next-command-event e)))
  306.                          e))))
  307.   (cond ((te-escape-p c)
  308.          (message "\"%s\" is escape char"))
  309.         ((and (eventp terminal-escape-char)
  310.               (event-to-character terminal-escape-char nil t nil))
  311.          (message "\"%s\" is now escape; \"%s\" passes though"
  312.                   (single-key-description c)
  313.                   (single-key-description terminal-escape-char)))
  314.         (t
  315.          (message "\"%s\" is now escape"
  316.                   (single-key-description c))
  317.          ;; Let mouse-events, for example, go back to looking at global map
  318.          (local-unset-key (vector terminal-escape-char))))
  319.   (local-set-key (vector c) 'te-escape) ;ensure it's defined
  320.   (setq terminal-escape-char c))
  321.  
  322.  
  323. (defun te-stuff-string (string)
  324.   "Read a string to send to through the terminal emulator
  325. as though that string had been typed on the keyboard.
  326.  
  327. Very poor man's file transfer protocol."
  328.   (interactive "sStuff string: ")
  329.   (process-send-string te-process string))
  330.  
  331. (defun te-set-output-log (name)
  332.   "Record output from the terminal emulator in a buffer."
  333.   (interactive (list (if te-log-buffer
  334.              nil
  335.                (read-buffer "Record output in buffer: "
  336.                     (format "%s output-log"
  337.                         (buffer-name (current-buffer)))
  338.                     nil))))
  339.   (if (or (null name) (equal name ""))
  340.       (progn (setq te-log-buffer nil)
  341.          (message "Output logging off."))
  342.     (if (get-buffer name)
  343.     nil
  344.       (save-excursion
  345.     (set-buffer (get-buffer-create name))
  346.     (fundamental-mode)
  347.     (buffer-disable-undo (current-buffer))
  348.     (erase-buffer)))
  349.     (setq te-log-buffer (get-buffer name))
  350.     (message "Recording terminal emulator output into buffer \"%s\""
  351.          (buffer-name te-log-buffer))))
  352.  
  353. (defun te-tofu ()
  354.   "Discontinue output log."
  355.   (interactive)
  356.   (te-set-output-log nil))
  357.   
  358.  
  359. (defun te-toggle (sym arg)
  360.   (set sym (cond ((not (numberp arg)) arg)
  361.          ((= arg 1) (not (symbol-value sym)))
  362.          ((< arg 0) nil)
  363.          (t t))))
  364.  
  365. (defun te-toggle-more-processing (arg)
  366.   (interactive "p")
  367.   (message (if (te-toggle 'terminal-more-processing arg)
  368.            "More processing on" "More processing off"))
  369.   (if terminal-more-processing (setq te-more-count -1)))
  370.  
  371. (defun te-toggle-scrolling (arg)
  372.   (interactive "p")
  373.   (message (if (te-toggle 'terminal-scrolling arg)
  374.            "Scroll at end of page" "Wrap at end of page")))
  375.  
  376. (defun te-enable-more-processing ()
  377.   "Enable ** MORE ** processing"
  378.   (interactive)
  379.   (te-toggle-more-processing t))
  380.  
  381. (defun te-disable-more-processing ()
  382.   "Disable ** MORE ** processing"
  383.   (interactive)
  384.   (te-toggle-more-processing nil))
  385.  
  386. (defun te-do-scrolling ()
  387.   "Scroll at end of page (yuck)"
  388.   (interactive)
  389.   (te-toggle-scrolling t))
  390.  
  391. (defun te-do-wrapping ()
  392.   "Wrap to top of window at end of page"
  393.   (interactive)
  394.   (te-toggle-scrolling nil))
  395.  
  396.  
  397. (defun te-set-redisplay-interval (arg)
  398.   "Set the maximum interval (in output characters) between screen updates.
  399. Set this number to large value for greater throughput,
  400. set it smaller for more frequent updates (but overall slower performance."
  401.   (interactive "NMax number of output chars between redisplay updates: ")
  402.   (setq arg (max arg 1))
  403.   (setq terminal-redisplay-interval arg
  404.     te-redisplay-count 0))
  405.  
  406. ;;;; more map
  407.  
  408. ;; every command -must- call te-more-break-unwind
  409. ;; or grave lossage will result
  410.  
  411. (put 'te-more-break-unread 'suppress-keymap t)
  412. (defun te-more-break-unread ()
  413.   (interactive)
  414.   (if (te-escape-p last-command-event)
  415.       (call-interactively 'te-escape)
  416.     (message "Continuing from more break (\"%s\" typed, %d chars output pending...)"
  417.          (single-key-description last-command-event)
  418.          (te-pending-output-length))
  419.     (setq te-more-count 259259)
  420.     (te-more-break-unwind)
  421.     (let ((terminal-more-processing nil))
  422.       (te-pass-through))))
  423.  
  424. (defun te-more-break-resume ()
  425.   "Proceed past the **MORE** break,
  426. allowing the next page of output to appear"
  427.   (interactive)
  428.   (message "Continuing from more break")
  429.   (te-more-break-unwind))
  430.  
  431. (defun te-more-break-help ()
  432.   "Provide help on commands available in a terminal-emulator **MORE** break"
  433.   (interactive)
  434.   (message "Terminal-emulator more break help...")
  435.   (sit-for 0)
  436.   (with-electric-help
  437.     (function (lambda ()
  438.       (princ "Terminal-emulator more break.\n\n")
  439.       (princ (format "Type \"%s\" (te-more-break-resume)\n%s\n"
  440.              (where-is-internal 'te-more-break-resume
  441.                     terminal-more-break-map t)
  442.              (documentation 'te-more-break-resume)))
  443.       (princ (substitute-command-keys "\\{terminal-more-break-map}\n"))
  444.       (princ "Any other key is passed through to the program
  445. running under the terminal emulator and disables more processing until
  446. all pending output has been dealt with.")
  447.       nil))))
  448.  
  449.  
  450. (defun te-more-break-advance-one-line ()
  451.   "Allow one more line of text to be output before doing another more break."
  452.   (interactive)
  453.   (setq te-more-count 1)
  454.   (te-more-break-unwind))
  455.  
  456. (defun te-more-break-flush-pending-output ()
  457.   "Discard any output which has been received by the terminal emulator but
  458. not yet proceesed and then proceed from the more break."
  459.   (interactive)
  460.   (te-more-break-unwind)
  461.   (te-flush-pending-output))
  462.  
  463. (defun te-flush-pending-output ()
  464.   "Discard any as-yet-unprocessed output which has been received by
  465. the terminal emulator."
  466.   (interactive)
  467.   ;; this could conceivably be confusing in the presence of
  468.   ;; escape-sequences spanning process-output chunks
  469.   (if (null (cdr te-pending-output))
  470.       (message "(There is no output pending)")
  471.     (let ((length (te-pending-output-length)))
  472.       (message "Flushing %d chars of pending output" length)
  473.       (setq te-pending-output
  474.         (list 0 (format "\n*** %d chars of pending output flushed ***\n"
  475.                 length)))
  476.       (te-update-pending-output-display)
  477.       (te-process-output nil)
  478.       (sit-for 0))))
  479.  
  480.  
  481. (defun te-pass-through ()
  482.   "Send the last character typed through the terminal-emulator
  483. without any interpretation"
  484.   (interactive)
  485.   (if (te-escape-p last-command-event)
  486.       (call-interactively 'te-escape)
  487.     (and terminal-more-processing
  488.      (null (cdr te-pending-output))
  489.      (te-set-more-count nil))
  490.     (let ((c (event-to-character last-command-event nil t nil)))
  491.       (if c (process-send-string te-process (make-string 1 c))))
  492.     (te-process-output t)))
  493.  
  494. (defun te-set-window-start ()
  495.   (let* ((w (get-buffer-window (current-buffer)))
  496.      (h (if w (window-height w))))
  497.     (cond ((not w)) ; buffer not displayed
  498.       ((>= h (/ (- (point) (point-min)) (1+ te-width)))
  499.        ;; this is the normal case
  500.        (set-window-start w (point-min)))
  501.       ;; this happens if some vandal shrinks our window.
  502.       ((>= h (/ (- (point-max) (point)) (1+ te-width)))
  503.        (set-window-start w (- (point-max) (* h (1+ te-width)) -1)))
  504.       ;; I give up.
  505.       (t nil))))
  506.  
  507. (defun te-pending-output-length ()
  508.   (let ((length (car te-pending-output))
  509.     (tem (cdr te-pending-output)))
  510.     (while tem
  511.       (setq length (+ length (length (car tem))) tem (cdr tem)))
  512.     length))
  513.  
  514. ;;;; more break hair
  515.  
  516. (defun te-more-break ()
  517.   (te-set-more-count t)
  518.   (make-local-variable 'te-more-old-point)
  519.   (setq te-more-old-point (point))
  520.   (make-local-variable 'te-more-old-local-map)
  521.   (setq te-more-old-local-map (current-local-map))
  522.   (use-local-map terminal-more-break-map)
  523.   (make-local-variable 'te-more-old-filter)
  524.   (setq te-more-old-filter (process-filter te-process))
  525.   (make-local-variable 'te-more-old-mode-line-format)
  526.   (setq te-more-old-mode-line-format mode-line-format
  527.     mode-line-format (list "--   **MORE**  "
  528.                    mode-line-buffer-identification
  529.                    "%-"))
  530.   (set-process-filter te-process
  531.     (function (lambda (process string)
  532.         (save-excursion
  533.           (set-buffer (process-buffer process))
  534.           (setq te-pending-output (nconc te-pending-output
  535.                          (list string))))
  536.           (te-update-pending-output-display))))
  537.   (te-update-pending-output-display)
  538.   (if (eq (window-buffer (selected-window)) (current-buffer))
  539.       (message "More break "))
  540.   (or (eobp)
  541.       (null terminal-more-break-insertion)
  542.       (save-excursion
  543.     (forward-char 1)
  544.     (delete-region (point) (+ (point) te-width))
  545.     (insert terminal-more-break-insertion)))
  546.   (run-hooks 'terminal-more-break-hook)
  547.   (sit-for 0) ;get display to update
  548.   (throw 'te-process-output t))
  549.  
  550. (defun te-more-break-unwind ()
  551.   (interactive)
  552.   (use-local-map te-more-old-local-map)
  553.   (set-process-filter te-process te-more-old-filter)
  554.   (goto-char te-more-old-point)
  555.   (setq mode-line-format te-more-old-mode-line-format)
  556.   (set-buffer-modified-p (buffer-modified-p))
  557.   (let ((buffer-read-only nil))
  558.     (cond ((eobp))
  559.       (terminal-more-break-insertion
  560.        (forward-char 1)
  561.        (delete-region (point)
  562.               (+ (point) (length terminal-more-break-insertion)))
  563.        (te-insert-blank te-width)
  564.        (goto-char te-more-old-point)))
  565.     (setq te-more-old-point nil)
  566.     (let ((te-more-count 259259))
  567.       (te-newline)))
  568.   ;(sit-for 0)
  569.   (te-process-output t))
  570.  
  571. (defun te-set-more-count (newline)
  572.   (let ((line (/ (- (point) (point-min)) (1+ te-width))))
  573.     (if newline (setq line (1+ line)))
  574.     (cond ((= line te-height)
  575.        (setq te-more-count te-height))
  576.       ;#### something is strange.  Investigate this!
  577.       ((= line (1- te-height))
  578.        (setq te-more-count te-height))
  579.       ((or (< line (/ te-height 2))
  580.            (> (- te-height line) 10))
  581.        ;; break at end of this page
  582.        (setq te-more-count (- te-height line)))
  583.       (t
  584.        ;; migrate back towards top (ie bottom) of screen.
  585.        (setq te-more-count (- te-height
  586.                   (if (> te-height 10) 2 1)))))))
  587.  
  588.  
  589. ;;;; More or less straight-forward terminal escapes
  590.  
  591. ;; ^j, meaning `newline' to non-display programs.
  592. ;; (Who would think of ever writing a system which doesn't understand
  593. ;;  display terminals natively?  Un*x:  The Operating System of the Future.)
  594. (defun te-newline ()
  595.   "Move down a line, optionally do more processing, perhaps wrap/scroll,
  596. move to start of new line, clear to end of line."
  597.   (end-of-line)
  598.   (cond ((not terminal-more-processing))
  599.     ((< (setq te-more-count (1- te-more-count)) 0)
  600.      (te-set-more-count t))
  601.     ((eq te-more-count 0)
  602.      ;; this doesn't return
  603.      (te-more-break)))
  604.   (if (eobp)
  605.       (progn
  606.     (delete-region (point-min) (+ (point-min) te-width))
  607.     (goto-char (point-min))
  608.     (if terminal-scrolling
  609.         (progn (delete-char 1)
  610.            (goto-char (point-max))
  611.            (insert ?\n))))
  612.     (forward-char 1)
  613.     (delete-region (point) (+ (point) te-width)))
  614.   (te-insert-blank te-width)
  615.   (beginning-of-line)
  616.   (te-set-window-start))
  617.  
  618. ;; ^p ^j
  619. ;; Handle the `do' or `nl' termcap capability.
  620. ;;#### I am not sure why this broken, obsolete, capability is here.
  621. ;;#### Perhaps it is for VIle.  No comment was made about why it
  622. ;;#### was added (in "Sun Dec  6 01:22:27 1987  Richard Stallman")
  623. (defun te-down-vertically-or-scroll ()
  624.   "Move down a line vertically, or scroll at bottom."
  625.   (let ((column (current-column)))
  626.     (end-of-line)
  627.     (if (eobp)
  628.     (progn
  629.       (delete-region (point-min) (+ (point-min) te-width))
  630.       (goto-char (point-min))
  631.       (delete-char 1)
  632.       (goto-char (point-max))
  633.       (insert ?\n)
  634.       (te-insert-blank te-width)
  635.       (beginning-of-line))
  636.       (forward-line 1))
  637.     (move-to-column column))
  638.   (te-set-window-start))
  639.  
  640. ; ^p = x+32 y+32
  641. (defun te-move-to-position ()
  642.   ;; must offset by #o40 since cretinous unix won't send a 004 char through
  643.   (let ((y (- (te-get-char) 32))
  644.     (x (- (te-get-char) 32)))
  645.     (if (or (> x te-width)
  646.         (> y te-height))
  647.     () ;(error "fucked %d %d" x y)
  648.       (goto-char (+ (point-min) x (* y (1+ te-width))))
  649.       ;(te-set-window-start?)
  650.       ))
  651.   (setq te-more-count -1))
  652.  
  653.  
  654.  
  655. ;; ^p c
  656. (defun te-clear-rest-of-line ()
  657.   (save-excursion
  658.     (let ((n (- (point) (progn (end-of-line) (point)))))
  659.       (delete-region (point) (+ (point) n))
  660.       (te-insert-blank (- n)))))
  661.  
  662.  
  663. ;; ^p C
  664. (defun te-clear-rest-of-screen ()
  665.   (save-excursion
  666.     (te-clear-rest-of-line)
  667.     (while (progn (end-of-line) (not (eobp)))
  668.       (forward-char 1) (end-of-line)
  669.       (delete-region (- (point) te-width) (point))
  670.       (te-insert-blank te-width))))
  671.       
  672.  
  673. ;; ^p ^l
  674. (defun te-clear-screen ()
  675.   ;; regenerate buffer to compensate for (nonexistent!!) bugs.
  676.   (erase-buffer)
  677.   (let ((i 0))
  678.     (while (< i te-height)
  679.       (setq i (1+ i))
  680.       (te-insert-blank te-width)
  681.       (insert ?\n)))
  682.   (delete-region (1- (point-max)) (point-max))
  683.   (goto-char (point-min))
  684.   (setq te-more-count -1))
  685.  
  686.  
  687. ;; ^p ^o count+32
  688. (defun te-insert-lines ()
  689.   (if (not (bolp))
  690.       ();(error "fooI")
  691.     (save-excursion
  692.       (let* ((line (- te-height (/ (- (point) (point-min)) (1+ te-width)) -1))
  693.          (n (min (- (te-get-char) ?\ ) line))
  694.          (i 0))
  695.     (delete-region (- (point-max) (* n (1+ te-width))) (point-max))
  696.     (if (eq (point) (point-max)) (insert ?\n))
  697.     (while (< i n)
  698.       (setq i (1+ i))
  699.       (te-insert-blank te-width)
  700.       (or (eq i line) (insert ?\n))))))
  701.   (setq te-more-count -1))
  702.  
  703.  
  704. ;; ^p ^k count+32
  705. (defun te-delete-lines ()
  706.   (if (not (bolp))
  707.       ();(error "fooD")
  708.     (let* ((line (- te-height (/ (- (point) (point-min)) (1+ te-width)) -1))
  709.        (n (min (- (te-get-char) ?\ ) line))
  710.        (i 0))
  711.       (delete-region (point)
  712.              (min (+ (point) (* n (1+ te-width))) (point-max)))
  713.       (save-excursion
  714.     (goto-char (point-max))
  715.     (while (< i n)
  716.       (setq i (1+ i))
  717.       (te-insert-blank te-width)
  718.       (or (eq i line) (insert ?\n))))))
  719.   (setq te-more-count -1))
  720.  
  721. ;; ^p ^a
  722. (defun te-beginning-of-line ()
  723.   (beginning-of-line))
  724.  
  725. ;; ^p ^b
  726. (defun te-backward-char ()
  727.   (if (not (bolp))
  728.       (backward-char 1)))
  729.  
  730. ;; ^p ^f
  731. (defun te-forward-char ()
  732.   (if (not (eolp))
  733.       (forward-char 1)))
  734.  
  735.  
  736. ;; ^p *
  737. (defun te-change-attribute ()
  738.   (let* ((attribute (te-get-char))
  739.          (on (= (te-get-char) ?1))
  740.          (standout (assq 'standout te-current-attributes))
  741.          (underline (assq 'underline te-current-attributes))
  742.          (frob (function (lambda ()
  743.                  ;; This would be even more of a combinatorial mess if I
  744.                  ;;  decided I wanted to support anything more than the two
  745.                  ;;  standout and underline attributes.
  746.                  (setq te-current-face
  747.                        (or (cdr (assoc te-current-attributes
  748.                                        '((((standout . t) (underline . nil))
  749.                                           . terminal-standout)
  750.                                          (((standout . nil) (underline . t))
  751.                                           . terminal-standout)
  752.                                          (((standout . t) (underline . nil))
  753.                                           . terminal-standout-underline))))
  754.                            'terminal-default))))))
  755.     (cond ((= attribute ?+) ;standout on/off
  756.            (setcdr standout on)
  757.            (funcall frob))
  758.           ((= attribute ?_) ;underline on/off
  759.            (setcdr underline on)
  760.            (funcall frob))
  761.           ;; reverse, blink, half-bright, double-bright, blank, protect
  762.           ;; ??Colours??
  763.           (t ;; #\space
  764.            (setcdr underline nil)
  765.            (setcdr standout nil)
  766.            (setq te-current-face 'terminal-default)))))
  767.  
  768.  
  769. ;; 0177
  770. (defun te-delete ()
  771.   (if (bolp)
  772.       ()
  773.     (delete-region (1- (point)) (point))
  774.     (te-insert-blank 1)
  775.     (forward-char -1)))
  776.  
  777. ;; ^p ^g
  778. (defun te-beep ()
  779.   (beep))
  780.  
  781.  
  782. ;; ^p _ count+32
  783. (defun te-insert-spaces ()
  784.   (let* ((p (point))
  785.      (n (min (- (te-get-char) 32)
  786.          (- (progn (end-of-line) (point)) p))))
  787.     (if (<= n 0)
  788.     nil
  789.       (delete-char (- n))
  790.       (goto-char p)
  791.       (insert-char ?\  n))
  792.     (goto-char p)))
  793.  
  794. ;; ^p d count+32  (should be ^p ^d but cretinous un*x won't send ^d chars!!!)
  795. (defun te-delete-char ()
  796.   (let* ((p (point))
  797.      (n (min (- (te-get-char) 32)
  798.          (- (progn (end-of-line) (point)) p))))
  799.     (if (<= n 0)
  800.     nil
  801.       (te-insert-blank n)
  802.       (goto-char p)
  803.       (delete-char n))
  804.     (goto-char p)))
  805.  
  806.  
  807.  
  808. ;; disgusting unix-required shit
  809. ;;  Are we living twenty years in the past yet?
  810.  
  811. (defun te-losing-unix ()
  812.   ;(what lossage)
  813.   ;(message "fucking-unix: %d" char)
  814.   )
  815.  
  816. ;; ^i
  817. (defun te-output-tab ()
  818.   (let* ((p (point))
  819.      (x (- p (progn (beginning-of-line) (point))))
  820.      (l (min (- 8 (logand x 7))
  821.          (progn (end-of-line) (- (point) p)))))
  822.     (goto-char (+ p l))))
  823.  
  824. ;; Also:
  825. ;;  ^m => beginning-of-line (for which it -should- be using ^p ^a, right?!!)
  826. ;;  ^g => te-beep (for which it should use ^p ^g)
  827. ;;  ^h => te-backward-char (for which it should use ^p ^b)
  828.  
  829.  
  830.  
  831. (defun te-filter (process string)
  832.   (let* ((obuf (current-buffer)))
  833.     ;; can't use save-excursion, as that preserves point, which we don't want
  834.     (unwind-protect
  835.     (progn
  836.       (set-buffer (process-buffer process))
  837.       (goto-char te-saved-point)
  838.       (and (bufferp te-log-buffer)
  839.            (if (null (buffer-name te-log-buffer))
  840.            ;; killed
  841.            (setq te-log-buffer nil)
  842.          (set-buffer te-log-buffer)
  843.          (goto-char (point-max))
  844.          (insert-before-markers string)
  845.          (set-buffer (process-buffer process))))
  846.       (setq te-pending-output (nconc te-pending-output (list string)))
  847.       (te-update-pending-output-display)
  848.           (te-process-output (eq (current-buffer)
  849.                                  (window-buffer (selected-window))))
  850.       (set-buffer (process-buffer process))
  851.       (setq te-saved-point (point)))
  852.       (set-buffer obuf))))
  853.  
  854. ;; fucking unix has -such- braindamaged lack of tty control...
  855. (defun te-process-output (preemptable)
  856.   ;;#### There seems no good reason to ever disallow preemption
  857.   (setq preemptable t)
  858.   (catch 'te-process-output
  859.     (let ((buffer-read-only nil)
  860.       (string nil) ostring start char (matchpos nil))
  861.       (while (cdr te-pending-output)
  862.     (setq ostring string
  863.           start (car te-pending-output)
  864.           string (car (cdr te-pending-output))
  865.           char (aref string start))
  866.     (if (eq (setq start (1+ start)) (length string))
  867.         (progn (setq te-pending-output
  868.                (cons 0 (cdr (cdr te-pending-output)))
  869.              start 0
  870.              string (car (cdr te-pending-output)))
  871.            (te-update-pending-output-display))
  872.         (setcar te-pending-output start))
  873.     (if (and (> char ?\037) (< char ?\377))
  874.         (cond ((eolp)
  875.            ;; unread char
  876.            (if (eq start 0)
  877.                (setq te-pending-output
  878.                  (cons 0 (cons (make-string 1 char)
  879.                        (cdr te-pending-output))))
  880.                (setcar te-pending-output (1- start)))
  881.            (te-newline))
  882.           ((null string)
  883.            (delete-char 1) (insert char)
  884.                    (put-text-property (1- (point)) (point)
  885.                                       'face te-current-face)
  886.            (te-redisplay-if-necessary 1))
  887.           (t
  888.            (let ((end (or (and (eq ostring string) matchpos)
  889.                   (setq matchpos (string-match
  890.                            "[\000-\037\177-\377]"
  891.                            string start))
  892.                   (length string))))
  893.              (delete-char 1) (insert char)
  894.              (setq char (point))
  895.                      (put-text-property (1- char) char 'face te-current-face)
  896.                      (end-of-line)
  897.              (setq end (min end (+ start (- (point) char))))
  898.              (goto-char char)
  899.              (if (eq end matchpos) (setq matchpos nil))
  900.              (delete-region (point) (+ (point) (- end start)))
  901.                      (setq char (point))
  902.              (insert (if (and (eq start 0)
  903.                       (eq end (length string)))
  904.                  string
  905.                      (substring string start end)))
  906.                      (put-text-property char (point) 'face te-current-face)
  907.              (if (eq end (length string))
  908.              (setq te-pending-output
  909.                    (cons 0 (cdr (cdr te-pending-output))))
  910.                  (setcar te-pending-output end))
  911.              (te-redisplay-if-necessary (1+ (- end start))))))
  912.       ;; I suppose if I split the guts of this out into a separate
  913.       ;;  function we could trivially emulate different terminals
  914.       ;; Who cares in any case?  (Apart from stupid losers using rlogin)
  915.       (funcall
  916.         (if (eq char ?\^p)
  917.             (or (cdr (assq (te-get-char)
  918.                    '((?= . te-move-to-position)
  919.                  (?c . te-clear-rest-of-line)
  920.                  (?C . te-clear-rest-of-screen)
  921.                  (?\C-o . te-insert-lines)
  922.                  (?\C-k . te-delete-lines)
  923.                                  (?* . te-change-attribute)
  924.                  ;; not necessary, but help sometimes.
  925.                  (?\C-a . te-beginning-of-line)
  926.                  (?\C-b . te-backward-char)
  927.                  ;; should be C-d, but un*x
  928.                  ;;  pty's won't send \004 through!
  929.                                  ;; Can you believe this?
  930.                  (?d . te-delete-char)
  931.                  (?_ . te-insert-spaces)
  932.                  ;; random
  933.                  (?\C-f . te-forward-char)
  934.                  (?\C-g . te-beep)
  935.                  (?\C-j . te-down-vertically-or-scroll)
  936.                  (?\C-l . te-clear-screen)
  937.                  )))
  938.             'te-losing-unix)
  939.             (or (cdr (assq char
  940.                    '((?\C-j . te-newline)
  941.                  (?\177 . te-delete)
  942.                  ;; Did I ask to be sent these characters?
  943.                  ;; I don't remember doing so, either.
  944.                  ;; (Perhaps some operating system or
  945.                  ;; other is completely incompetent...)
  946.                  (?\C-m . te-beginning-of-line) ;fuck me harder
  947.                  (?\C-g . te-beep)             ;again and again!
  948.                  (?\C-h . te-backward-char)     ;wa12id!!
  949.                  (?\C-i . te-output-tab))))     ;(spiked)
  950.             'te-losing-unix)))              ;That feels better
  951.       (te-redisplay-if-necessary 1))
  952.     (and preemptable
  953.          (input-pending-p)
  954.          ;; preemptable output!  Oh my!!
  955.          (throw 'te-process-output t)))))
  956.   ;; We must update window-point in every window displaying our buffer
  957.   (let* ((s (selected-window))
  958.      (w s))
  959.     (while (not (eq s (setq w (next-window w))))
  960.       (if (eq (window-buffer w) (current-buffer))
  961.       (set-window-point w (point))))))
  962.  
  963. (defun te-get-char ()
  964.   (if (cdr te-pending-output)
  965.       (let ((start (car te-pending-output))
  966.         (string (car (cdr te-pending-output))))
  967.     (prog1 (aref string start)
  968.       (if (eq (setq start (1+ start)) (length string))
  969.           (setq te-pending-output (cons 0 (cdr (cdr te-pending-output))))
  970.           (setcar te-pending-output start))))
  971.     (catch 'char
  972.       (let ((filter (process-filter te-process)))
  973.     (unwind-protect
  974.         (progn
  975.           (set-process-filter te-process
  976.                   (function (lambda (p s)
  977.                                     (or (eq (length s) 1)
  978.                                         (setq te-pending-output (list 1 s)))
  979.                                     (throw 'char (aref s 0)))))
  980.           (accept-process-output te-process))
  981.       (set-process-filter te-process filter))))))
  982.  
  983.  
  984. (defun te-redisplay-if-necessary (length)
  985.   (and (<= (setq te-redisplay-count (- te-redisplay-count length)) 0)
  986.        (eq (current-buffer) (window-buffer (selected-window)))
  987.        (waiting-for-user-input-p)
  988.        (progn (te-update-pending-output-display)
  989.           (sit-for 0)
  990.           (setq te-redisplay-count terminal-redisplay-interval))))
  991.  
  992. (defun te-update-pending-output-display ()
  993.   (if (null (cdr te-pending-output))
  994.       (setq te-pending-output-info "")      
  995.     (let ((length (te-pending-output-length)))
  996.       (if (< length 1500)
  997.       (setq te-pending-output-info "")
  998.     (setq te-pending-output-info (format "(%dK chars output pending) "
  999.                          (/ (+ length 512) 1024))))))
  1000.   ;; update mode line
  1001.   (set-buffer-modified-p (buffer-modified-p)))
  1002.  
  1003.  
  1004. (defun te-sentinel (process message)
  1005.   (cond ((eq (process-status process) 'run))
  1006.     ((null (buffer-name (process-buffer process)))) ;deleted
  1007.     (t (let ((b (current-buffer)))
  1008.          (save-excursion
  1009.            (set-buffer (process-buffer process))
  1010.            (setq buffer-read-only nil)
  1011.            (fundamental-mode)
  1012.            (goto-char (point-max))
  1013.            (delete-blank-lines)
  1014.            (delete-horizontal-space)
  1015.            (insert "\n*******\n" message "*******\n"))
  1016.          (if (and (eq b (process-buffer process))
  1017.               (waiting-for-user-input-p))
  1018.          (progn (goto-char (point-max))
  1019.             (recenter -1)))))))
  1020.  
  1021. (defvar te-stty-string "stty -nl erase '^?' kill '^u' intr '^c' echo pass8"
  1022.   "Shell command to set terminal modes for terminal emulator.")
  1023. ;; This used to have `new' in it, but that loses outside BSD
  1024. ;; and it's apparently not needed in BSD.
  1025.  
  1026. (defvar explicit-shell-file-name nil
  1027.   "*If non-nil, is file name to use for explicitly requested inferior shell.")
  1028.  
  1029. ;;;###autoload
  1030. (defun terminal-emulator (buffer program args &optional width height)
  1031.   "Under a display-terminal emulator in BUFFER, run PROGRAM on arguments ARGS.
  1032. ARGS is a list of argument-strings.  Remaining arguments are WIDTH and HEIGHT.
  1033. BUFFER's contents are made an image of the display generated by that program,
  1034. and any input typed when BUFFER is the current Emacs buffer is sent to that
  1035. program an keyboard input.
  1036.  
  1037. Interactively, BUFFER defaults to \"*terminal*\" and PROGRAM and ARGS
  1038. are parsed from an input-string using your usual shell.
  1039. WIDTH and HEIGHT are determined from the size of the current window
  1040. -- WIDTH will be one less than the window's width, HEIGHT will be its height.
  1041.  
  1042. To switch buffers and leave the emulator, or to give commands
  1043. to the emulator itself (as opposed to the program running under it),
  1044. type Control-^.  The following character is an emulator command.
  1045. Type Control-^ twice to send it to the subprogram.
  1046. This escape character may be changed using the variable `terminal-escape-char'.
  1047.  
  1048. `Meta' characters may not currently be sent through the terminal emulator.
  1049.  
  1050. Here is a list of some of the variables which control the behaviour
  1051. of the emulator -- see their documentation for more information:
  1052. terminal-escape-char, terminal-scrolling, terminal-more-processing,
  1053. terminal-redisplay-interval.
  1054.  
  1055. This function calls the value of terminal-mode-hook if that exists
  1056. and is non-nil after the terminal buffer has been set up and the
  1057. subprocess started.
  1058.  
  1059. Presently with `termcap' only; if somebody sends us code to make this
  1060. work with `terminfo' we will try to use it."
  1061.   (interactive
  1062.     (cons (save-excursion
  1063.         (set-buffer (get-buffer-create "*terminal*"))
  1064.         (buffer-name (if (or (not (boundp 'te-process))
  1065.                  (null te-process)
  1066.                  (not (eq (process-status te-process)
  1067.                       'run)))
  1068.                  (current-buffer)
  1069.                (generate-new-buffer "*terminal*"))))
  1070.       (append
  1071.         (let* ((default-s
  1072.              ;; Default shell is same thing M-x shell uses.
  1073.              (or explicit-shell-file-name
  1074.              (getenv "ESHELL")
  1075.              (getenv "SHELL")
  1076.              "/bin/sh"))
  1077.            (s (read-shell-command
  1078.                (format "Run program in emulator: (default %s) "
  1079.                    default-s))))
  1080.           (if (equal s "")
  1081.           (list default-s '())
  1082.                   (te-parse-program-and-args s))))))
  1083.   (switch-to-buffer buffer)
  1084.   (if (null width) (setq width (- (window-width (selected-window)) 1)))
  1085.   (if (null height) (setq height (- (window-height (selected-window)) 1)))
  1086.   (terminal-mode)
  1087.   (setq te-width width te-height height)
  1088.   (setq mode-line-buffer-identification
  1089.     (list (format "Emacs terminal %dx%d: %%b  " te-width te-height)
  1090.           'te-pending-output-info))
  1091.   (let ((buffer-read-only nil))
  1092.     (te-clear-screen))
  1093.   (let (process)
  1094.     (while (setq process (get-buffer-process (current-buffer)))
  1095.       (if (y-or-n-p (format "Kill process %s? " (process-name process)))
  1096.       (delete-process process)
  1097.     (error "Process %s not killed" (process-name process)))))
  1098.   (condition-case err
  1099.       (let ((termcap
  1100.              ;; Because of Unix Brain Death(tm), we can't change
  1101.              ;;  the terminal type of a running process, and so
  1102.              ;;  terminal size and scrollability are wired-down
  1103.              ;;  at this point.  ("Detach?  What's that?")
  1104.              (concat (format "emacs-virtual:co#%d:li#%d:%s:km:"
  1105.                              ;; Sigh.  These can't be dynamically changed.
  1106.                              te-width te-height (if terminal-scrolling
  1107.                                                     "" "ns:"))
  1108.                      ;;-- Basic things
  1109.                      ;; cursor-motion, bol, forward/backward char
  1110.                      "cm=^p=%+ %+ :cr=^p^a:le=^p^b:nd=^p^f:"
  1111.                      ;; newline, clear eof/eof, audible bell
  1112.                      "nw=^j:ce=^pc:cd=^pC:cl=^p^l:bl=^p^g:"
  1113.                      ;; insert/delete char/line
  1114.                      "IC=^p_%+ :DC=^pd%+ :AL=^p^o%+ :DL=^p^k%+ :"
  1115.                      ;;-- Not-widely-known (ie nonstandard) flags, which mean
  1116.                      ;; o writing in the last column of the last line
  1117.                      ;;   doesn't cause idiotic scrolling, and
  1118.                      ;; o don't use idiotische c-s/c-q sogenannte
  1119.                      ;;   ``flow control'' auf keinen Fall.
  1120.                      "LP:NF:"
  1121.                      ;;-- For stupid or obsolete programs
  1122.                      "ic=^p_!:dc=^pd!:al=^p^o!:dl=^p^k!:ho=^p=  :"
  1123.                      ;;-- For disgusting programs.
  1124.                      ;; (VI? What losers need these, I wonder?)
  1125.                      "im=:ei=:dm=:ed=:mi:do=^p^j:nl=^p^j:bs:"
  1126.                      "ms:me=^p*  :"
  1127.                      (if (face-equal 'terminal-default 'terminal-standout)
  1128.                          "" "so=^p*+1:se=^p*+0")
  1129.                      (if (face-equal 'terminal-default 'terminal-underline)
  1130.                          "" "us=^p*_1:ue=^p*_0")
  1131.                      )))
  1132.     (if (fboundp 'start-subprocess)
  1133.         ;; this winning function would do everything, except that
  1134.         ;;  rms doesn't want it.
  1135.         (setq te-process (start-subprocess "terminal-emulator"
  1136.                    program args
  1137.                    'channel-type 'terminal
  1138.                    'filter 'te-filter
  1139.                    'buffer (current-buffer)
  1140.                    'sentinel 'te-sentinel
  1141.                    'modify-environment
  1142.                      (list (cons "TERM" "emacs-virtual")
  1143.                        (cons "TERMCAP" termcap))))
  1144.       ;; so instead we resort to this...
  1145.       (setq te-process
  1146.                 (let ((process-environment
  1147.                        (cons "TERM=emacs-virtual"
  1148.                              (cons (concat "TERMCAP=" termcap)
  1149.                                    process-environment))))
  1150.                   (start-process "terminal-emulator"
  1151.                                  (current-buffer)
  1152.                                  "/bin/sh" "-c"
  1153.                                  ;; Yuck!!! Start a shell to set some
  1154.                                  ;; terminal control characteristics.
  1155.                                  ;; Then exec the program we wanted.
  1156.                                  (format "%s; exec %s"
  1157.                                          te-stty-string
  1158.                                          (mapconcat 'te-quote-arg-for-sh
  1159.                                                     (cons program args)
  1160.                                                     " ")))))
  1161.       (set-process-filter te-process 'te-filter)
  1162.       (set-process-sentinel te-process 'te-sentinel)))
  1163.     (error (fundamental-mode)
  1164.        (signal (car err) (cdr err))))
  1165.   ;; sigh
  1166.   (setq inhibit-quit t)            ;sport death
  1167.   (use-local-map terminal-map)
  1168.   (run-hooks 'terminal-mode-hook)
  1169.   (message "Entering emacs terminal-emulator...  Type %s %s for help"
  1170.        (single-key-description terminal-escape-char)
  1171.        (mapconcat 'single-key-description
  1172.               (where-is-internal 'te-escape-help
  1173.                      terminal-escape-map
  1174.                      t)
  1175.               " ")))
  1176.  
  1177.  
  1178. (defun te-parse-program-and-args (s)
  1179.   (cond ((string-match "\\`\\([a-zA-Z0-9-+=_.@/:]+[ \t]*\\)+\\'" s)
  1180.      (let ((l ()) (p 0))
  1181.        (while p
  1182.          (setq l (cons (if (string-match
  1183.                 "\\([a-zA-Z0-9-+=_.@/:]+\\)\\([ \t]+\\)*"
  1184.                 s p)
  1185.                    (prog1 (substring s p (match-end 1))
  1186.                  (setq p (match-end 0))
  1187.                  (if (eq p (length s)) (setq p nil)))
  1188.                    (prog1 (substring s p)
  1189.                  (setq p nil)))
  1190.                l)))
  1191.        (setq l (nreverse l))
  1192.        (list (car l) (cdr l))))
  1193.     ((and (string-match "[ \t]" s) (not (file-exists-p s)))
  1194.      (list shell-file-name (list "-c" (concat "exec " s))))
  1195.     (t (list s ()))))
  1196.  
  1197. (put 'terminal-mode 'mode-class 'special)
  1198. ;; This is only separated out from function terminal-emulator
  1199. ;; to keep the latter a little more managable.
  1200. (defun terminal-mode ()
  1201.   "Set up variables for use f the terminal-emualtor.
  1202. One should not call this -- it is an internal function
  1203. of the terminal-emulator"
  1204.   (kill-all-local-variables)
  1205.   (buffer-disable-undo (current-buffer))
  1206.   (setq major-mode 'terminal-mode)
  1207.   (setq mode-name "terminal")
  1208. ; (make-local-variable 'Helper-return-blurb)
  1209. ; (setq Helper-return-blurb "return to terminal simulator")
  1210.   (setq mode-line-process '(": %s"))
  1211.   (setq buffer-read-only t)
  1212.   (setq truncate-lines t)
  1213.   (make-local-variable 'terminal-escape-char)
  1214.   (setq terminal-escape-char (default-value 'terminal-escape-char))
  1215.   (make-local-variable 'terminal-scrolling)
  1216.   (setq terminal-scrolling (default-value 'terminal-scrolling))
  1217.   (make-local-variable 'terminal-more-processing)
  1218.   (setq terminal-more-processing (default-value 'terminal-more-processing))
  1219.   (make-local-variable 'terminal-redisplay-interval)
  1220.   (setq terminal-redisplay-interval (default-value 'terminal-redisplay-interval))
  1221.   (make-local-variable 'te-width)
  1222.   (make-local-variable 'te-height)
  1223.   (make-local-variable 'te-process)
  1224.   (make-local-variable 'te-pending-output)
  1225.   (setq te-pending-output (list 0))
  1226.   (make-local-variable 'te-saved-point)
  1227.   (setq te-saved-point (point-min))
  1228.   (make-local-variable 'te-pending-output-info) ;for the mode line
  1229.   (setq te-pending-output-info "")
  1230.   (make-local-variable 'inhibit-quit)
  1231.   ;(setq inhibit-quit t)
  1232.   (make-local-variable 'te-log-buffer)
  1233.   (setq te-log-buffer nil)
  1234.   (make-local-variable 'te-more-count)
  1235.   (setq te-more-count -1)
  1236.   (make-local-variable 'te-redisplay-count)
  1237.   (setq te-redisplay-count terminal-redisplay-interval)
  1238.   (make-local-variable 'te-current-face)
  1239.   (setq te-current-face 'terminal-default)
  1240.   (make-local-variable 'te-current-attributes)
  1241.   (setq te-current-attributes (list (cons 'standout nil)
  1242.                                     (cons 'underline nil)))
  1243.   ;(use-local-map terminal-mode-map)
  1244.   ;; terminal-mode-hook is called above in function terminal-emulator
  1245.   (make-local-variable 'meta-prefix-char)
  1246.   (setq meta-prefix-char -1)            ;death to ASCII lossage
  1247.   )
  1248.  
  1249. ;;;; what a complete loss
  1250.  
  1251. (defun te-quote-arg-for-sh (fuckme)
  1252.   (cond ((string-match "\\`[a-zA-Z0-9-+=_.@/:]+\\'"
  1253.                fuckme)
  1254.      fuckme)
  1255.     ((not (string-match "[$]" fuckme))
  1256.      ;; "[\"\\]" are special to sh and the lisp reader in the same way
  1257.      (prin1-to-string fuckme))
  1258.     (t
  1259.      (let ((harder "")
  1260.            (cretin 0)
  1261.            (stupid 0))
  1262.        (while (cond ((>= cretin (length fuckme))
  1263.              nil)
  1264.             ;; this is the set of chars magic with "..." in `sh'
  1265.             ((setq stupid (string-match "[\"\\$]"
  1266.                             fuckme cretin))
  1267.              t)
  1268.             (t (setq harder (concat harder
  1269.                         (substring fuckme cretin)))
  1270.                nil))
  1271.          (setq harder (concat harder (substring fuckme cretin stupid)
  1272.                                   ;; Can't use ?\\ since `concat'
  1273.                                   ;; unfortunately does prin1-to-string
  1274.                                   ;; on fixna.  Amazing.
  1275.                   "\\"
  1276.                   (substring fuckme
  1277.                          stupid
  1278.                          (1+ stupid)))
  1279.            cretin (1+ stupid)))
  1280.        (concat "\"" harder "\"")))))
  1281.